<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>声明周期</title>
</head>
<body>
    <button>来个看图神器</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/17/react.development.js"></script>
    <script src="../js/17/react-dom.development.js"></script>
    <script src="../js/17/babel.min.js"></script>

    <script type="text/babel">
        //
        class App extends React.Component{
            state = {
                url: 'http://api.xiaohigh.com/image/random'
            }

            render(){
                return <img width="500" src={this.state.url} alt=""  onClick={this.change} />
            }

            change = () => {
                this.setState({
                    url: 'http://api.xiaohigh.com/image/random?t=' + Date.now()
                })
            }

            //get 获取  derived 派生, 衍生  state 状态  from 来自于  props 属性
            //特点: 使用 props 来作为 state 的值
            static getDerivedStateFromProps(props, state){
                // console.log('xxx');
                return {
                    url: 'https://t7.baidu.com/it/u=1956604245,3662848045&fm=193&f=GIF'
                }
            }

            //实例方法
            //get 获取  snapshot  快照  before 在...之前  update 更新
            //作用: 获取页面更新的状态数据 DOM 数据
            getSnapshotBeforeUpdate(){
                return '当前的滚动距离为 100';
            }

            //更新完毕  获取 getSnapshotBeforeUpdate 返回值
            componentDidUpdate(props, state, res){
                console.log(props)
                console.log(state)
                console.log(res);
            }

        }

        //获取两个按钮
        let btns = document.querySelectorAll('button');

        //第一个按钮单击事件
        btns[0].onclick = function(){
            //挂载 渲染组件
            ReactDOM.render(<App />, document.querySelector("#root"));
        }

        //第二个按钮单击事件
        btns[1].onclick = function(){
            //卸载组件  unmount 卸载  component 组件  at 在   node 节点
            ReactDOM.unmountComponentAtNode(document.querySelector("#root"));
        }
    </script>
</body>
</html>